package com.ljc.cjgl.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ljc.cjgl.custom.FileUtil;
import com.ljc.cjgl.custom.TablePageable;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.Student;
import com.ljc.cjgl.entitiy.TbClass;
import com.ljc.cjgl.form.StudentForm;
import com.ljc.cjgl.service.StudentService;

@Controller
@RequestMapping(value="/student")
public class StudentController {
	@Autowired
	private StudentService service;
	
	@RequestMapping(value="/manage")
	public String manage() {
		return "student/manage";
	}
	
	@RequestMapping(value="/edit")
	public String edit(Integer id,String bjMc,ModelMap map) {
		Student model=new Student();
		if(id!=null) {
			model=service.findById(id);
		}
		map.put("model", model);
		List<TbClass> bjs=new ArrayList<TbClass>();
		if(StringUtils.hasText(bjMc)) {
			bjs=service.findByTbClassNameLike(bjMc);
		}else {
			bjs=service.findAllTbClass();
		}
		map.put("bjs", bjs);
		return "student/edit";
	}
	
	@RequestMapping(value="page")
	@ResponseBody
	public Object page(TablePageable pageable,StudentForm
			form,HttpServletRequest request) {
		PageRequest pageRequest = pageable.bulidPageRequest();
		HashSet<Rule> rules=new HashSet<>();
		String name=form.getName();
		String bjMc=form.getBjMc();
		String search=form.getSearch();
		if(StringUtils.hasText(name)) {
			rules.add(Rule.property("name").contain(name));
		}
		if(StringUtils.hasText(bjMc)) {
			rules.add(Rule.property("tbClass.name").contain(bjMc));
		}
		if(StringUtils.hasText(search)) {
			HashSet<Rule> rules2=new HashSet<>();
			rules2.add(Rule.property("name").contain(search));
			rules2.add(Rule.property("tbClass.name").contain(search));
			rules2.add(Rule.property("address").contain(search));
			rules2.add(Rule.property("phone").contain(search));
			rules2.add(Rule.property("stuNum").contain(search));
			rules2.add(Rule.property("addressPhone").contain(search));
			rules.add(Rule.or(rules2));
		}
		Page<Student> page = service.findAll(Rule.and(rules).toSpecification(), pageRequest);
		return TableResult.bulidPageResult(page);
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(MultipartFile file,StudentForm form,HttpServletRequest request) throws IOException {
		service.save(file,form);
		return TableResult.bulidResult(true, "数据保存成功");
	}
	
	@RequestMapping(value="/download")
	@ResponseBody
	public Object download(HttpServletRequest request,HttpServletResponse response,Integer id,String uid) {
		Student student=null;
		if(id!=null) {
			student = service.findById(id);
		}else {
			student=service.findByUid(uid);
		}
		FileUtil.download(request,response,student.getSaveName(),student.getPhotoName());
		return null;
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(@RequestBody List<Student> students) {
		HashMap<String, Object> result=new HashMap<>(); 
		if(service.delete(students)) {
			result.put("success", true);
			result.put("msg", "数据删除成功");
		}else {
			result.put("success", false);
			result.put("msg", "数据删除失败");
		}
		return result;
	}
	
	
}
